package by.alex.dzeshko.facebooksuperclient.service;

import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import by.alex.dzeshko.facebooksuperclient.ContextHolder;
import by.alex.dzeshko.facebooksuperclient.FeedMainActivity;
import by.alex.dzeshko.facebooksuperclient.async.NewPostTask;
import by.alex.dzeshko.facebooksuperclient.async.common.ParamCallback;
import by.alex.dzeshko.facebooksuperclient.utils.Notifier;

public class PostService extends CommonService<JSONObject> implements ParamCallback<JSONObject>{

	private static final String ID = "id";

	private static final String OK = "Ok";

	private static final String VALUE = "value";

	private static final String TYPE = "type";

	private static final String ERROR2 = "Error";

	private static final String MESSAGE = "message";

	private static final String ERROR = "error";

	private static final String SENDING = "Sending...";

	public static final String ACTION_ON_ERROR = "by.alex.dzeshko.facebooksuperclient.service.PostService.ACTION_ON_ERROR";
	
	public static final String ACTION_ON_SUCCESS = "by.alex.dzeshko.facebooksuperclient.service.PostService.ACTION_ON_SUCCESS";

	public static final String POST_ON_SUCCESS = "by.alex.dzeshko.facebooksuperclient.service.PostService.POST_ON_SUCCESS";

	private static final String LOG_TAG = "PostService";
	
	NotificationManager mNotifyManager;
	
	@Override
	public void onCreate() {
		super.onCreate();
		mNotifyManager = (NotificationManager) ContextHolder.getInstance().getContext().getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	protected void handleCommand(Intent intent) {
		super.handleCommand(intent);
		setNotification(SENDING, SENDING);
		new NewPostTask(this).start(requestBuilder);
	}

	private void setNotification(String title, String message) {
		Intent intent = new Intent(this, FeedMainActivity.class);
    	final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
		NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(getApplicationContext());
		notiBuilder.setContentTitle(title)
					.setContentInfo(title)
					.setTicker(title)
					.setContentText(message)
					.setContentIntent(pendingIntent)
					.setSmallIcon(R.drawable.ic_menu_upload)
					.setAutoCancel(true);
		mNotifyManager.notify(123, notiBuilder.build());
		
	}

	@Override
	protected void threadedHandleResponse(JSONObject response) {
		if(response.has(ERROR)){
			JSONObject err;
			try {
				err = response.getJSONObject(ERROR);
				String errMessage = err.getString(MESSAGE);
				setNotification(ERROR2, errMessage);
				Log.e(err.getString(TYPE),errMessage);
				onError(new Throwable(errMessage));
				return;
			} catch (JSONException e) {
				onError(e);
			}
		}
		else if(response.has(VALUE)){
			try {
				if(response.getBoolean(VALUE))
				setNotification(OK, OK);
				mNotifyManager.cancel(123);
			} catch (JSONException e) {
				Log.e(LOG_TAG, e.toString());
				onError(new Throwable(e));
			}
			Intent intent = new Intent(ACTION_ON_SUCCESS);
			sendBroadcast(intent);
		}
		else if(response.has(ID)){
			setNotification(OK, OK);
			mNotifyManager.cancel(123);
			Intent intent = new Intent(POST_ON_SUCCESS);
			sendBroadcast(intent);
		}
	}

	@Override
	public void onSuccess(JSONObject p) {
		onHandleResponse(p);
	}

	@Override
	public void onError(Throwable e) {
		Intent intent = new Intent(ACTION_ON_ERROR);
		intent.putExtra(EXTRA_KEY_MESSAGE, e.getMessage());
		sendBroadcast(intent);
	}
}
